﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
//using System.Collections;
using System.Xml;
using System.Web.Services;

public partial class Services_OutManage : ATPages
{
    Function f = new Function();
    ZSql zs = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initgvData();
            initUser();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            hfxs.Value = Request.QueryString["moduid"].ToString();
            yjTm1.Text = Request.QueryString["yjTm1"];
            yjTm2.Text = Request.QueryString["yjTm2"];
            catalogueName.Text = Request.QueryString["catalogueName"];
            comodBarcode.Text = Request.QueryString["comodBarcode"];
            OutCode.Text = Request.QueryString["OutCode"];
            maintmen.SelectedValue = Request.QueryString["maintmen"];
        }
    }

    private void initgvData() 
    {

        string search = "";
        string sql = "select * from (select a.*,(select top 1 TypeName from SV_Type where a.TypeId=TypeId) as TypeName,(select top 1 catalogueName from SV_catalogue where a.ComodBarcode=ComodBarcode) as catalogueName,(select top 1 StaffName from CS_Staff where StaffId=a.maintmen ) as StaffName from SV_OutStock a ) TblOutStock where 1=1 ";
        if (!string.IsNullOrEmpty(Request.QueryString["yjTm1"]) && !string.IsNullOrEmpty(Request.QueryString["yjTm2"]))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
            sql += " and CONVERT(char(10), EditDate, 120)>='" + Request.QueryString["yjTm1"] + "' and CONVERT(char(10), EditDate, 120)<='" + Request.QueryString["yjTm2"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["catalogueName"]))
        {
            search += "&catalogueName=" + Common.FormatParameter(Request.QueryString["catalogueName"].Trim());
            sql += " and catalogueName like'%" + Request.QueryString["catalogueName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(Request.QueryString["ComodBarcode"].Trim());
            sql += " and ComodBarcode like'%" + Request.QueryString["ComodBarcode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["OutCode"]))
        {
            search += "&OutCode=" + Common.FormatParameter(Request.QueryString["OutCode"].Trim());
            sql += " and OutCode like'%" + Request.QueryString["OutCode"] + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["maintmen"]))
        {
            search += "&maintmen=" + Common.FormatParameter(Request.QueryString["maintmen"].Trim());
            sql += " and maintmen =" + Request.QueryString["maintmen"] + "";
        }

        sql += " order by OutStockId desc";
        PageNavigator1.Url = "../Services/OutManage.aspx?moduid=" + Request.QueryString["moduid"].ToString() + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnSelec_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
        }
        if (!string.IsNullOrEmpty(comodBarcode.Text.Trim()))
        {
            search += "&comodBarcode=" + Common.FormatParameter(comodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(catalogueName.Text.Trim()))
        {
            search += "&catalogueName=" + catalogueName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(OutCode.Text.Trim()))
        {
            search += "&OutCode=" + Common.FormatParameter(OutCode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(maintmen.Text.Trim()))
        {
            search += "&maintmen=" + Common.FormatParameter(maintmen.Text.Trim());
        }
        
        PageNavigator1.Url = "../Services/OutManage.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("OutManage.aspx?moduid=" + hfxs.Value + search);
    }


    //生成用户
    private void initUser()
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=40 order by StaffName");
        maintmen.DataSource = name.m_table;
        maintmen.DataTextField = "StaffName";
        maintmen.DataValueField = "StaffId";
        maintmen.DataBind();
        maintmen.Items.Insert(0, new ListItem("所有", ""));
        name.Close();
    }

    protected void Edit_Click(object sender, EventArgs e)
    {
        string OutStockId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("OutManageEdit.aspx?OutStockId=" + OutStockId + "&action=edit&moduid=" + Request["moduid"] + "");

    }
    protected void Label6_Click(object sender, EventArgs e)
    {
        string SalesCode = (sender as LinkButton).CommandArgument.ToString();
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + SalesCode + "')", true);

    }
    protected void GvDataBand_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int typeSale = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "AuditState"));
            int isEnd = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "isEnd"));
            if (typeSale == 0)
            {
                e.Row.Cells[14].Text = "未审核";
            }
            else if (typeSale == 1)
            {
                e.Row.Cells[14].Text = "已通过";
            }
            else if (typeSale == 2)
            {
                e.Row.Cells[14].Text = "未通过";
            }
            //可用状态
            if (isEnd == 1)
            {
                e.Row.Cells[13].Text = "已用";
                (e.Row.FindControl("ckFlag") as CheckBox).Enabled = false;//删除是否可用
            }
            else
            {
                e.Row.Cells[13].Text = "未用";
            }
        }

    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = this.GvData.Rows[i].Cells[0].Text;
                    zs.Open("delete SV_OutStock  where OutStockId='" + id + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)//审核操作
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update SV_OutStock set AuditState=" + flag + " where OutStockId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("OutStock.aspx?moduid=122");
    }
    protected void Excel_Click(object sender, EventArgs e)
    {
        
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=OutManage" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }
}
